#!/bin/sh

# File        :  preinstall
#
# Purpose     :  execute all tasks necessary prior to installation of Privoxy's files
#
# Copyright   :  Written by and Copyright (C) 2001-2012 the
#                Privoxy team. http://www.privoxy.org/
#
#                This program is free software; you can redistribute it
#                and/or modify it under the terms of the GNU General
#                Public License as published by the Free Software
#                Foundation; either version 2 of the License, or (at
#                your option) any later version.
#
#                This program is distributed in the hope that it will
#                be useful, but WITHOUT ANY WARRANTY; without even the
#                implied warranty of MERCHANTABILITY or FITNESS FOR A
#                PARTICULAR PURPOSE.  See the GNU General Public
#                License for more details.
#
#                The GNU General Public License should be included with
#                this file.  If not, you can view it at
#                http://www.gnu.org/copyleft/gpl.html
#                or write to the Free Software Foundation, Inc.,
#                51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
#                USA
#
# Modification : If you modify this file please consider whether your 
#                changes ought to be passed back to the macsetup module.
#

#  This preinstall script:
#  1. Shuts down Privoxy if it's running
#  2. Detects the version of OS X on which we're installing
#  3. Creates the privoxy group and user
#  4. Backs up any existing log & configuration files

# create installation log file
logfile='/var/privoxy_installation.log'
echo 'Privoxy installation on' `/bin/date` > ${logfile}

# 1. Stop the Privoxy service if it's running
#
# The following will only return a value if there is a process whose name contains '/privoxy'
# We need the / so as to exclude the scripts that'll be running during installtion
privoxy_pid=`ps -ax | grep /privoxy | grep -v grep | awk '{ print $1 }'`
# if privoxy_pid is a number (reroute stderr to dump "integer expression expected" error which'll be thrown if Privoxy isn't running)
if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
	# try using the appropriate startup method to stop it (else it might simply restart)
	if [ `/bin/launchctl list | /usr/bin/grep "org.ijbswa.privoxy" | /usr/bin/awk '{print $3}'` ]; then
		echo 'Stopping Privoxy via LaunchDaemon.' >> ${logfile}
		/bin/launchctl unload /Library/LaunchDaemons/org.ijbswa.privoxy.plist >> ${logfile} 2>&1
		# give Privoxy a chance to shut down
		sleep 3
	fi
	privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
	if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
		if [ -f /Library/StartupItems/Privoxy/Privoxy ]; then
			echo 'Stopping Privoxy via StartupItem.' >> ${logfile}
			/Library/StartupItems/Privoxy/Privoxy stop >> ${logfile} 2>&1
			# give Privoxy a chance to shut down
			sleep 3
		fi
		# If neither of the above shutdown methods (installed by a prior 'approved' installer) work,
		# then shut down Privoxy the Unix way
		privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
		if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
			echo 'Privoxy not shutting down via OS X startup methods, doing it the Unix way.' >> ${logfile}
			/bin/kill -TERM "${privoxy_pid}" >> ${logfile} 2>&1
			# give Privoxy a chance to shut down
			sleep 3
			# if Privoxy is still running then terminate it aggressively
			privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
			if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
				echo 'Privoxy still not shutting down so must be hung. Killing it aggressively.' >> ${logfile}
				/bin/kill -KILL "${privoxy_pid}" >> ${logfile} 2>&1
				/bin/rm -f /var/run/privoxy.pid
			fi
		fi
	fi
else
	echo 'Privoxy not running.' >> ${logfile}
fi

# 2. Detect the version of OS X on which we're installing
#
darwin_major_rel_num="`/usr/bin/uname -r | /usr/bin/sed 's/\..*//'`"
echo 'Darwin major release version is:' ${darwin_major_rel_num} >> ${logfile}

# 3. Create the privoxy group and user (using the host-specific command to do so)
#
case "${darwin_major_rel_num}" in
  # Mac OS X 10.5 or higher
  9|1*)
    GNAME="`/usr/bin/dscl /Local/Default -list /groups | /usr/bin/grep -E '^(_)?privoxy?'`"
    UNAME="`/usr/bin/dscl /Local/Default -list /users | /usr/bin/grep -E '^(_)?privoxy?'`"
	# create group if it does not exist
	if [ -z ${GNAME} ]; then
	  GID1="`/usr/bin/dscl /Local/Default -list /groups gid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
	  Privoxy_GID="`/bin/expr ${GID1} + 1`"
		echo 'Creating privoxy group using dscl' >> ${logfile}
	  /usr/bin/dscl /Local/Default -create /Groups/_privoxy >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -append /Groups/_privoxy RecordName privoxy >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Groups/_privoxy Password "*" >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Groups/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
		/usr/bin/dscl /Local/Default -create /Groups/_privoxy RealName "privoxy users" >> ${logfile} 2>&1
	fi
	# create user if it does not exist
	if [ -z ${UNAME} ]; then
	  UID1="`/usr/bin/dscl /Local/Default -list /users uid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
	  Privoxy_UID="`/bin/expr ${UID1} + 1`"
		echo 'Creating privoxy user using dscl' >> ${logfile}
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -append /Users/_privoxy RecordName privoxy >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy NFSHomeDirectory /var/empty >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy Password "*" >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy RealName "privoxy server" >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy UniqueID ${Privoxy_UID} >> ${logfile} 2>&1
	  /usr/bin/dscl /Local/Default -create /Users/_privoxy UserShell /usr/bin/false >> ${logfile} 2>&1
	fi
  ;;
  # Mac OS X 10.4, 10.3
  8|7)
    GNAME="`/usr/bin/nireport . /groups name | /usr/bin/grep -E '^privoxy?'`"
    UNAME="`/usr/bin/nireport . /users name | /usr/bin/grep -E '^privoxy?'`"
	# create group if it does not exist
	if [ -z ${GNAME} ]; then
	  GID1="`/usr/bin/nireport . /groups gid | /usr/bin/sort -n | /usr/bin/tail -1`"
	  Privoxy_GID="`/bin/expr ${GID1} + 1`"
		echo 'Creating privoxy group using nicl' >> ${logfile}
	  /usr/bin/nicl . -create /groups/privoxy >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /groups/privoxy passwd "*" >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /groups/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
	fi
	# create user if it does not exist
	if [ -z ${UNAME} ]; then
	  UID1="`/usr/bin/nireport . /users uid | /usr/bin/sort -n | /usr/bin/tail -1`"
	  Privoxy_UID="`/bin/expr ${UID1} + 1`"
		echo 'Creating privoxy user using nicl' >> ${logfile}
	  /usr/bin/nicl . -create /users/privoxy >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy passwd "*" >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy uid ${Privoxy_UID} >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy change 0 >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy expire 0 >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy realname "Privoxy Web Proxy" >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy home /var/empty >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy shell /usr/bin/false >> ${logfile} 2>&1
	  /usr/bin/nicl . -create /users/privoxy _shadow_passwd "" >> ${logfile} 2>&1
	fi
  ;;
  # default
  *)
    GNAME=""
    UNAME=""
    ;;
esac

# 4. Back up any existing log & configuration files
#
if [ -f /var/log/privoxy/logfile.log ]; then
	echo 'Backing up existing logfile' >> ${logfile}
  /bin/cp /var/log/privoxy/logfile.log /var/log/privoxy/logfile.old >> ${logfile} 2>&1
	/usr/sbin/chown privoxy:privoxy /var/log/privoxy/logfile.old >> ${logfile} 2>&1
fi
if [ -d /usr/local/etc/privoxy ]; then
	for i in config match-all.action trust user.action user.filter; do
		echo 'Backing up existing config file:' ${i} >> ${logfile}
		if [ -f /usr/local/etc/privoxy/$i ]; then
			/bin/cp /usr/local/etc/privoxy/$i /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
			/usr/sbin/chown privoxy:privoxy /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
		fi
	done
	echo 'Backing up existing config templates' >> ${logfile}
	/bin/cp -R /usr/local/etc/privoxy/templates /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
	/usr/sbin/chown -R privoxy:privoxy /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
fi

exit 0
